// index.js
// 获取应用实例
import {
  banners,
  personalized,
  toplist,
  playListDetail
} from '../../api/index/index';
const app = getApp()

Page({
  data: {
    bannerList: [], // 轮播图列表
    personalizedList: [], // 推荐歌单
    topSongList: [] // 排行榜单
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  async onLoad() {
    // 获取banner
    let bannerListData = await banners({
      type: 1
    });
    // 获取精心推荐
    let personalizedListData = await personalized({
      limit: 10,
    })
    // 获取排行榜
    let topListData = await toplist();
    // 截取前4个排行旁
    let topLists = topListData.list.splice(0, 4);

    // 循环获取每个排行榜的歌曲
    for (let index = 0; index < topLists.length; index++) {
      const element = topLists[index];
      this.getListDetail(element);
    }

    this.setData({
      bannerList: bannerListData.banners,
      personalizedList: personalizedListData.result,
    });
  },
  async getListDetail(item) {
    const list = await playListDetail({
      id: item.id
    });
    let songList = this.data.topSongList;
    songList.push({
      name: list.playlist.name,
      id: list.playlist.id,
      coverImgUrl: list.playlist.coverImgUrl,
      tracks: list.playlist.tracks.splice(0, 3)
    });
    this.setData({
      topSongList: songList
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})